package prg421.samples.week1;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class HelloWorld extends JFrame
{
	// Assign a unique version to this application. Optional.
	private static final long serialVersionUID = -3946449349033949915L;

	public HelloWorld()
	{
		// Add title to JFrame and exit the application when the [x] title bar button is clicked
		// (otherwise, the application will continue to run while its window is closed).
		super("Hello");
	    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Create the components.
		JLabel helloLabel = new JLabel("Hello, World!", SwingConstants.CENTER);
		JButton exitButton = new JButton("Exit");
		
		// Make exit button listen and take action when pressed.
		exitButton.addActionListener(new ActionListener() 
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}		
		});
		
		// Chooose a layout manager.
		setLayout(new BorderLayout());
		
		// Add the components to the JFrame container.
		add(helloLabel, BorderLayout.CENTER);
		add(exitButton, BorderLayout.SOUTH);
	}
	
	public static void createAndShowGUI()
	{
		HelloWorld frame = new HelloWorld();
		frame.pack();
		frame.setSize(400,300);
		frame.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				createAndShowGUI();
			}
		});
	}
}
